package com.feeyo.groundservice.composewidget.view.list

import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.gestures.ScrollableDefaults
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import com.feeyo.groundservice.composewidget.view.image.CustomAsyncImage

/**
 *作者：QLH on 2023-07-27
 *描述：
 */
/**
 * 图片网格布局,这是封装好的，也可以单独使用 CustomAsyncImage 控制细致布局
 * **/
@Composable
fun ImageGridListView(
    columns: GridCells = GridCells.Adaptive(60.dp),//默认自适应
    modifier: Modifier = Modifier,
    imageLoader: ImageLoader? = null,
    state: LazyGridState = rememberLazyGridState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    reverseLayout: Boolean = false,
    verticalArrangement: Arrangement.Vertical =
        if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
    userScrollEnabled: Boolean = true,
    //针对单个item设置的
    images: List<String?>,
    isPre: Boolean = true,
    itemHeight: Dp = 60.dp,
    contentScale: ContentScale = ContentScale.FillBounds
) {
    LazyVerticalGrid(
        columns,
        modifier,
        state,
        contentPadding,
        reverseLayout,
        verticalArrangement,
        horizontalArrangement,
        flingBehavior,
        userScrollEnabled
    ) {
        itemsIndexed(images) { index, item ->
            CustomAsyncImage(
                model = item,
                imageLoader = imageLoader,
                contentDescription = null,
                modifier = Modifier.height(itemHeight).fillMaxWidth(),
                contentScale = contentScale,
                currentPosition = if (isPre) index else null,
                images = images
            )
        }
    }
}